/**
 * 
 */
package com.precisosoft.mathematize.server.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.LinkedList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.xml.DOMConfigurator;
import org.json.JSONObject;

import com.precisosoft.mathematize.server.model.UserProfile;


/**
 * @author jewettdx
 *
 */
public class LoginServlet extends HttpServlet {
	 
	 private static final long serialVersionUID = 8032611514671727168L;
	 
	 private static UserProfile userProfile;
	 
	 static {
//		 UUID uuid = UUID.randomUUID();
//	     String uid = uuid.toString();
		 userProfile = new UserProfile("1", "TestUser", "testuser@gmail.com", "2012-11-20");
		 }

	 @Override
	 protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
	  
	  try {
	   
		DOMConfigurator.configure("log4j.xml");
		  
		String tag =  req.getParameter("tag");
		String email =  req.getParameter("email");
		String password =  req.getParameter("password");

	   JSONObject responseObj = new JSONObject();
	   responseObj.put("success", "1");
	   
	   List<JSONObject> loginObjects = new LinkedList<JSONObject>();
	   JSONObject userObj = new JSONObject();
	   userObj.put("uid", userProfile.getUid());
	   userObj.put("name", userProfile.getName());
	   userObj.put("email", userProfile.getEmail());
	   userObj.put("created_at", userProfile.getCreated_at());
	   loginObjects.add(userObj);
	   responseObj.put("user", loginObjects);
	   
	   PrintWriter writer = resp.getWriter();
	   writer.write(responseObj.toString());
	   writer.flush();
	   
	  } 
	  catch (Exception e) {   
	   e.printStackTrace();
	   throw new ServletException(e);
	  }
	  
	 }

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		doGet(req, resp);
	}

}
